using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class buy : System.Web.UI.Page
{
    DataTable dtCats;

    #region Properties
    string Keyword
    {
        get { return ViewState["Keyword"].ToString(); }
        set { ViewState["Keyword"] = value; }
    }

    int CurPage
    {
        get { return Convert.ToInt32(lblCurPage.Text); }
        set { lblCurPage.Text = value.ToString(); }
    }  
    #endregion

    #region Helper Methods
    private void DoCat(decimal catID)
    {
        DataTable dtFloors = new FloorBus().LoadUndoneFloorsInCat(catID);

        int curPg = Convert.ToInt32(lblCurPage.Text);
        DataPager pg = new DataPager(dtFloors, curPg - 1);
        DataTable dt = pg.Table;

        if (dt == null || dt.Rows.Count == 0)
        {
            lblSearchRs.Visible = true;
            ShowHidePagingLabels(false);
        }
        else
        {
            lblSearchRs.Visible = false;
            ShowHidePagingLabels(true);
            dlFloors.DataSource = dt;
            dlFloors.DataBind();
        }

        lblPages.Text = pg.PagesString;
        ShowHidePagingButtons(curPg);
    }

    private void DoSearch()
    {
        //string kw = txtKw.Text;
        string kw = this.Keyword;
        DataTable src = new FloorBus().SearchFloorsByName(kw);

        int curPg = Convert.ToInt32(lblCurPage.Text);
        DataPager pg = new DataPager(src, curPg - 1);
        DataTable dt = pg.Table;

        if (dt.Rows.Count == 0) lblSearchRs.Visible = true;
        else lblSearchRs.Visible = false;

        dlFloors.DataSource = dt;
        dlFloors.DataBind();

        lblPages.Text = pg.PagesString;
        ShowHidePagingLabels(!lblSearchRs.Visible);
        ShowHidePagingButtons(curPg);
    }

    void ShowHidePagingButtons(int curPg)
    {
        try
        {
            int pages = Convert.ToInt32(lblPages.Text);
            btnPrev.Visible = (curPg > 1);
            btnNext.Visible = (curPg < pages);
        }
        catch { }
    }

    void ShowHidePagingLabels(bool state)
    {
        lblPrefix.Visible = state;
        lblCurPage.Visible = state;
        lblSuffix.Visible = state;
        lblPages.Visible = state;
    }

    private void RefreshPage()
    {
        if (Request.QueryString.Count == 0) DoSearch();
        else
        {
            decimal catID = Convert.ToDecimal(Request.QueryString["CatID"]);
            DoCat(catID);
        }
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        UserUtils.ValidateLogin();

        //get catID
        decimal catID = 0;
        if (Request.QueryString.Count > 0)
        {
            catID = Convert.ToDecimal(Request.QueryString["CatID"]);
        }

        //reload categories tree
        dtCats = new CategoryBus().LoadCategories(catID);
        if (dtCats.Rows.Count == 0) lblCatRs.Visible = true;
        else
        {
            lblCatRs.Visible = false;
            dlCats.DataSource = dtCats;
            dlCats.DataBind();
        }

        //load undone floors in category
        if (Request.Form.Count == 0) DoCat(catID);
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.CurPage = 1;
        this.Keyword = txtKw.Text;
        DoSearch();
    }

    protected void btnNext_Click(object sender, ImageClickEventArgs e)
    {
        this.CurPage++;
        RefreshPage();
    }

    protected void btnPrev_Click(object sender, ImageClickEventArgs e)
    {
        this.CurPage--;
        RefreshPage();
    }
}
